
## 
#' Activate package environment
using Pkg
Pkg.activate(".")
Pkg.status()
##


## 
"Arrow" ∉ keys(Pkg.project().dependencies) && Pkg.add("Arrow")
"CSV" ∉ keys(Pkg.project().dependencies) && Pkg.add("CSV")
"XLSX" ∉ keys(Pkg.project().dependencies) && Pkg.add("XLSX")
"DataFrames" ∉ keys(Pkg.project().dependencies) && Pkg.add("DataFrames")
"Chain" ∉ keys(Pkg.project().dependencies) && Pkg.add("Chain")
"Dates" ∉ keys(Pkg.project().dependencies) && Pkg.add("Dates")
"Plots" ∉ keys(Pkg.project().dependencies) && Pkg.add("Plots")
"PrettyTables" ∉ keys(Pkg.project().dependencies) && Pkg.add("PrettyTables")
"StatsModels" ∉ keys(Pkg.project().dependencies) && Pkg.add("StatsModels")
"GLM" ∉ keys(Pkg.project().dependencies) && Pkg.add("GLM")
"FixedEffectModels" ∉ keys(Pkg.project().dependencies) && Pkg.add("FixedEffectModels")
"ShiftedArrays" ∉ keys(Pkg.project().dependencies) && Pkg.add("ShiftedArrays")
"Statistics" ∉ keys(Pkg.project().dependencies) && Pkg.add("Statistics")
"TerminalPager" ∉ keys(Pkg.project().dependencies) && Pkg.add("TerminalPager")
"FloatingTableView" ∉ keys(Pkg.project().dependencies) && Pkg.add("FloatingTableView")
"TexTables" ∉ keys(Pkg.project().dependencies) && Pkg.add("TexTables")
"Distributions" ∉ keys(Pkg.project().dependencies) && Pkg.add("Distributions")
"DataStructures" ∉ keys(Pkg.project().dependencies) && Pkg.add("DataStructures")
"CovarianceMatrices" ∉ keys(Pkg.project().dependencies) && Pkg.add("CovarianceMatrices", rev="master")
##

## 
#' load packages
using Arrow, CSV, XLSX, DataFrames, Chain, Dates, PrettyTables, StatsModels, GLM, FixedEffectModels, 
    ShiftedArrays, Statistics, TerminalPager, FloatingTableView, TexTables, Distributions, DataStructures, CovarianceMatrices
##



##
# Load Data

#' Load credit card data
file =raw"Data_Table_05.arrow"
arrTbl= DataFrame(Arrow.Table(file))
Returns = copy(arrTbl)
arrTbl = []
select!(Returns, Between(:Year, :Return))
sort!(Returns,[:Year,:Month,:IssuerNo,:TrustNo,:Tranche])


# #aggregate returns to monthly
Returns_m = @chain Returns begin
    groupby([:Year,:Month,:Tranche])
    combine(:Return => mean, renamecols=false)
end
Returns_m = unstack(Returns_m,[:Year,:Month], :Tranche, :Return, renamecols= (x->"Rx_" * x))
sort!(Returns_m,[:Year,:Month])
disallowmissing!(Returns_m)


#load Fama French
FF = CSV.File("./FamaFrenchMonthly.csv"; missingstring="-999") |> DataFrame
Returns_m = leftjoin(Returns_m,FF,on=[:Year,:Month])
disallowmissing!(Returns_m)

#load CMT
CMT = CSV.File("./CMT.csv"; missingstring="-999") |> DataFrame
transform!(CMT, names(CMT,Not([:Year,:Month])) .=> (x->x./100),renamecols=false)
Returns_m = leftjoin(Returns_m,CMT, on=[:Year,:Month])
disallowmissing!(Returns_m,Between(:CMT_3m,:CMT_10y))


#load corp bond TR indices (monthly returns)
TRCorp = CSV.File("CorpTRIndices.csv",missingstring="-999") |> DataFrame
Returns_m = leftjoin(Returns_m,TRCorp,on=[:Year,:Month])
##



##
# join datasets
function getSubperiod(x)
    if x>=2000 && x<2007
        return "2000-2006"
    elseif x>=2007 && x<2010
        return "2007-2009"
    elseif x>=2010 && x<2021
        return "2010-2020"
    end
end
function missMean(x)
    if all(ismissing.(x))
        return missing
    else
        return mean(skipmissing(x))
    end
end
RegData = @chain Returns_m begin
    groupby([:Year,:Month])
    combine([:Rx_A, :Rx_B, :Rx_C, :RxCorpAaa, :RxCorpBaa, :RxCorpHY] .=> missMean, [:Rf, :RxExMkt, :SMB, :HML, :MOM] .=> first, renamecols=false)
    transform([:Rx_A,:Rf] => (-) => :RxEx_A, [:Rx_B,:Rf] => (-) => :RxEx_B, [:Rx_C,:Rf] => (-) => :RxEx_C,
              [:RxCorpAaa,:Rf] => (-) => :RxExCorpAaa,
              [:RxCorpBaa,:Rf] => (-) => :RxExCorpBaa, 
              [:RxCorpHY,:Rf] => (-) => :RxExCorpHY) 
    transform(:Year => ByRow(x->getSubperiod(x)) => :Subperiod)
end
##



##########################################################################################################################
## overall summary statistics

Sumstat_Sub = @chain RegData begin
    groupby(:Subperiod)
    combine([:Rx_A,:Rx_B,:Rx_C,:RxEx_A,:RxEx_B,:RxEx_C, :RxCorpAaa, :RxCorpBaa, :RxCorpHY,
                :RxExCorpAaa, :RxExCorpBaa, :RxExCorpHY] .=> 
                (x->missMean(x)*100*12) .=> [:Rx_A,:Rx_B,:Rx_C,:RxEx_A,:RxEx_B,:RxEx_C, :RxCorpAaa, :RxCorpBaa, :RxCorpHY,
                :RxExCorpAaa, :RxExCorpBaa, :RxExCorpHY],
                [:Rx_A,:Rx_B,:Rx_C,:RxEx_A,:RxEx_B,:RxEx_C, :RxCorpAaa, :RxCorpBaa, :RxCorpHY,
                :RxExCorpAaa, :RxExCorpBaa, :RxExCorpHY] .=> 
                (x->std(skipmissing(x))*100*sqrt(12.0)) .=> [:SDevRx_A,:SDevRx_B,:SDevRx_C,:SDevRxEx_A,:SDevRxEx_B,:SDevRxEx_C, :SDevRxCorpAaa, :SDevRxCorpBaa, :SDevRxCorpHY,
                :SDevRxExCorpAaa, :SDevRxExCorpBaa, :SDevRxExCorpHY])
    transform!([:RxEx_A,:SDevRx_A] => ByRow( (rex,sdev)->rex/sdev)=> :SR_A, [:RxEx_B,:SDevRx_B] => ByRow( (rex,sdev)->rex/sdev)=> :SR_B, [:RxEx_C,:SDevRx_C] => ByRow( (rex,sdev)->rex/sdev)=> :SR_C,
               [:RxExCorpAaa,:SDevRxCorpAaa] => ByRow( (rex,sdev)->rex/sdev)=> :SR_CorpAaa,
               [:RxExCorpBaa,:SDevRxCorpBaa] => ByRow( (rex,sdev)->rex/sdev)=> :SR_CorpBaa,
               [:RxExCorpHY,:SDevRxCorpHY] => ByRow( (rex,sdev)->rex/sdev)=> :SR_CorpHY)
end
Sumstat_All = @chain RegData begin
    select(Not(:Subperiod))
    transform(:Year => ByRow(x->"All") => :Subperiod)
    groupby(:Subperiod)
    combine([:Rx_A,:Rx_B,:Rx_C,:RxEx_A,:RxEx_B,:RxEx_C, :RxCorpAaa, :RxCorpBaa, :RxCorpHY,
                :RxExCorpAaa, :RxExCorpBaa, :RxExCorpHY] .=> 
                (x->missMean(x)*100*12) .=> [:Rx_A,:Rx_B,:Rx_C,:RxEx_A,:RxEx_B,:RxEx_C, :RxCorpAaa, :RxCorpBaa, :RxCorpHY,
                :RxExCorpAaa, :RxExCorpBaa, :RxExCorpHY],
                [:Rx_A,:Rx_B,:Rx_C,:RxEx_A,:RxEx_B,:RxEx_C, :RxCorpAaa, :RxCorpBaa, :RxCorpHY,
                :RxExCorpAaa, :RxExCorpBaa, :RxExCorpHY] .=> 
                (x->std(skipmissing(x))*100*sqrt(12.0)) .=> [:SDevRx_A,:SDevRx_B,:SDevRx_C,:SDevRxEx_A,:SDevRxEx_B,:SDevRxEx_C, :SDevRxCorpAaa, :SDevRxCorpBaa, :SDevRxCorpHY,
                :SDevRxExCorpAaa, :SDevRxExCorpBaa, :SDevRxExCorpHY])
    transform!([:RxEx_A,:SDevRx_A] => ByRow( (rex,sdev)->rex/sdev)=> :SR_A, [:RxEx_B,:SDevRx_B] => ByRow( (rex,sdev)->rex/sdev)=> :SR_B, [:RxEx_C,:SDevRx_C] => ByRow( (rex,sdev)->rex/sdev)=> :SR_C,
               [:RxExCorpAaa,:SDevRxCorpAaa] => ByRow( (rex,sdev)->rex/sdev)=> :SR_CorpAaa,
               [:RxExCorpBaa,:SDevRxCorpBaa] => ByRow( (rex,sdev)->rex/sdev)=> :SR_CorpBaa,
               [:RxExCorpHY,:SDevRxCorpHY] => ByRow( (rex,sdev)->rex/sdev)=> :SR_CorpHY)
end
Sumstat = [Sumstat_All; Sumstat_Sub]
Sumstat = DataFrame([[names(Sumstat)]; collect.(eachrow(Sumstat))], [:Return; Symbol.(Sumstat[:,1])])
Sumstat = Sumstat[2:end,:]



#Results in Table 5 (excluding market beta)
# Note: Rows ordered manually from console output to match Table 5
# │ Return           All       2000-2006  2007-2009  2010-2020 
# ┼────────────────────────────────────────────────────────────
# │ RxEx_A           1.94718   2.05051    2.72643    1.58089
# │ RxEx_B           3.09933   2.31432    6.6251     2.4816
# │ RxEx_C           4.52906   3.21103    10.0544    3.6339
# │ RxExCorpAaa      4.24727   4.0223     1.38465    5.46454
# │ RxExCorpBaa      5.14084   3.86306    5.05416    6.2326
# │ RxExCorpHY       5.74306   4.31037    5.21183    7.12344
# ┼────────────────────────────────────────────────────────────
# │ SDevRx_A         3.18828   0.875896   7.63896    0.873739
# │ SDevRx_B         7.90219   0.767927   19.4955    1.38443
# │ SDevRx_C         11.5015   1.22771    28.402     1.86132
# │ SDevRxCorpAaa    5.75346   4.60663    8.78842    5.26738
# │ SDevRxCorpBaa    5.94007   5.42536    9.93989    4.28775
# │ SDevRxCorpHY     9.2368    8.0341     17.1722    5.49086
# ┼────────────────────────────────────────────────────────────
# │ SR_A             0.61073   2.34104    0.356911   1.80934
# │ SR_B             0.392212  3.01373    0.339826   1.79251
# │ SR_C             0.393779  2.61546    0.354005   1.95232
# │ SR_CorpAaa       0.738211  0.873155   0.157554   1.03743
# │ SR_CorpBaa       0.865451  0.712038   0.508472   1.45358
# │ SR_CorpHY        0.621759  0.536509   0.303504   1.29733
# ┼────────────────────────────────────────────────────────────




################################################################################

#function to make table columns
function TableCol_tStat(header, m::RegressionModel;
    stats=(:N=>Int∘nobs, "\$R^2\$"=>r2,"Adj. \$R^2\$"=>adjr2),
    meta=(), stderror::Function=stderror, kwargs...)

    # Compute p-values
    pval(m) = ccdf.(FDist(1, dof_residual(m)),
        abs2.(coef(m)./stderror(m)))

    # Initialize the column
    col  = RegCol(header)

    # Add the coefficients
    for (name, val, se, p) in zip(coefnames(m), coef(m), stderror(m), pval(m))
        tStat = val/se
        setcoef!(col, name, val, tStat)
        0.05 <  p <= .1  && star!(col[name], 1)
        0.01 <  p <= .05 && star!(col[name], 2)
        p <= .01 && star!(col[name], 2) #Note: not more than 2 stars
    end

    # Add in the fit statistics
    setstats!(col, OrderedDict(p.first=>p.second(m) for p in stats))

    # Add in the metadata
    setmeta!(col, OrderedDict(p.first=>p.second(m) for p in meta))

    return col
end


##################################################################################################################################
#Run market regressions

RegData_All = RegData
RegData_Pre = filter(:Subperiod => ==("2000-2006"),RegData_All)
RegData_Crisis = filter(:Subperiod => ==("2007-2009"),RegData_All)
RegData_Post = filter(:Subperiod => ==("2010-2020"),RegData_All)



#All 
f_A = @formula(RxEx_A ~ 1 + RxExMkt)
mod_A = lm(f_A,RegData_All)
f_B = @formula(RxEx_B ~ 1 + RxExMkt)
mod_B = lm(f_B,RegData_All)
f_C = @formula(RxEx_C ~ 1 + RxExMkt)
mod_C = lm(f_C,RegData_All)
f_Aaa = @formula(RxExCorpAaa ~ 1 + RxExMkt)
mod_Aaa = lm(f_Aaa,RegData_All)
f_Baa = @formula(RxCorpBaa ~ 1 + RxExMkt)
mod_Baa = lm(f_Baa,RegData_All)
f_HY = @formula(RxExCorpHY ~ 1 + RxExMkt)
mod_HY = lm(f_HY,RegData_All)
alphaA = coef(mod_A)[1].*12.0
alphaB = coef(mod_B)[1].*12.0
alphaC = coef(mod_C)[1].*12.0
alphaAaa = coef(mod_Aaa)[1].*12.0
alphaBaa = coef(mod_Baa)[1].*12.0
alphaHY = coef(mod_HY)[1].*12.0
alpha = [alphaA, alphaB, alphaC,alphaAaa,alphaBaa,alphaHY]
betaA = coef(mod_A)[2]
betaB = coef(mod_B)[2]
betaC = coef(mod_C)[2]
betaAaa = coef(mod_Aaa)[2]
betaBaa = coef(mod_Baa)[2]
betaHY = coef(mod_HY)[2]
beta = [betaA, betaB, betaC,betaAaa,betaBaa,betaHY]
AlphaBeta_All = DataFrame(Alpha=alpha, Beta=beta)

@fmt Real = "{:.6f}"
nwStd(m) = stderror(BartlettKernel(5), m, prewhite = false)
tA = TableCol_tStat("(A Tranche)", mod_A; stderror=nwStd)
setmeta!(tA, :α => "$alphaA")
tB = TableCol_tStat("(B Tranche)", mod_B; stderror=nwStd)
setmeta!(tB, :α => "$alphaB")
tC = TableCol_tStat("(C Tranche)", mod_C; stderror=nwStd)
setmeta!(tC, :α => "$alphaC")
tAaa = TableCol_tStat("(Corp Aaa)", mod_Aaa; stderror=nwStd)
setmeta!(tAaa, :α => "$alphaAaa")
tBaa = TableCol_tStat("(Corp Baa)", mod_Baa; stderror=nwStd)
setmeta!(tBaa, :α => "$alphaBaa")
tHY   = TableCol_tStat("(Corp HY)", mod_HY; stderror=nwStd)
setmeta!(tHY, :α => "$alphaHY")
tbl = regtable(tA,tB,tC,tAaa,tBaa,tHY)
print(to_ascii(tbl,se_pos=:inline,pad=1))


#Pre 
f_A = @formula(RxEx_A ~ 1 + RxExMkt)
mod_A = lm(f_A,RegData_Pre)
f_B = @formula(RxEx_B ~ 1 + RxExMkt)
mod_B = lm(f_B,RegData_Pre)
f_C = @formula(RxEx_C ~ 1 + RxExMkt)
mod_C = lm(f_C,RegData_Pre)
f_Aaa = @formula(RxExCorpAaa ~ 1 + RxExMkt)
mod_Aaa = lm(f_Aaa,RegData)
f_Baa = @formula(RxCorpBaa ~ 1 + RxExMkt)
mod_Baa = lm(f_Baa,RegData)
f_HY = @formula(RxExCorpHY ~ 1 + RxExMkt)
mod_HY = lm(f_HY,RegData)
alphaA = coef(mod_A)[1].*12.0
alphaB = coef(mod_B)[1].*12.0
alphaC = coef(mod_C)[1].*12.0
alphaAaa = coef(mod_Aaa)[1].*12.0
alphaBaa = coef(mod_Baa)[1].*12.0
alphaHY = coef(mod_HY)[1].*12.0
alpha = [alphaA, alphaB, alphaC,alphaAaa,alphaBaa,alphaHY]
betaA = coef(mod_A)[2]
betaB = coef(mod_B)[2]
betaC = coef(mod_C)[2]
betaAaa = coef(mod_Aaa)[2]
betaBaa = coef(mod_Baa)[2]
betaHY = coef(mod_HY)[2]
beta = [betaA, betaB, betaC,betaAaa,betaBaa,betaHY]
AlphaBeta_Pre = DataFrame(Alpha=alpha, Beta=beta)


@fmt Real = "{:.6f}"
nwStd(m) = stderror(BartlettKernel(5), m, prewhite = false)
tA = TableCol_tStat("(A Tranche)", mod_A; stderror=nwStd)
setmeta!(tA, :α => "$alphaA")
tB = TableCol_tStat("(B Tranche)", mod_B; stderror=nwStd)
setmeta!(tB, :α => "$alphaB")
tC = TableCol_tStat("(C Tranche)", mod_C; stderror=nwStd)
setmeta!(tC, :α => "$alphaC")
tAaa = TableCol_tStat("(Corp Aaa)", mod_Aaa; stderror=nwStd)
setmeta!(tAaa, :α => "$alphaAaa")
tBaa = TableCol_tStat("(Corp Baa)", mod_Baa; stderror=nwStd)
setmeta!(tBaa, :α => "$alphaBaa")
tHY   = TableCol_tStat("(Corp HY)", mod_HY; stderror=nwStd)
setmeta!(tHY, :α => "$alphaHY")
tbl = regtable(tA,tB,tC,tAaa,tBaa,tHY)
print(to_ascii(tbl,se_pos=:inline,pad=1))


#Crisis
f_A = @formula(RxEx_A ~ 1 + RxExMkt)
mod_A = lm(f_A,RegData_Crisis)
f_B = @formula(RxEx_B ~ 1 + RxExMkt)
mod_B = lm(f_B,RegData_Crisis)
f_C = @formula(RxEx_C ~ 1 + RxExMkt)
mod_C = lm(f_C,RegData_Crisis)
f_Aaa = @formula(RxExCorpAaa ~ 1 + RxExMkt)
mod_Aaa = lm(f_Aaa,RegData_Crisis)
f_Baa = @formula(RxCorpBaa ~ 1 + RxExMkt)
mod_Baa = lm(f_Baa,RegData_Crisis)
f_HY = @formula(RxExCorpHY ~ 1 + RxExMkt)
mod_HY = lm(f_HY,RegData_Crisis)
alphaA = coef(mod_A)[1].*12.0
alphaB = coef(mod_B)[1].*12.0
alphaC = coef(mod_C)[1].*12.0
alphaAaa = coef(mod_Aaa)[1].*12.0
alphaBaa = coef(mod_Baa)[1].*12.0
alphaHY = coef(mod_HY)[1].*12.0
alpha = [alphaA, alphaB, alphaC,alphaAaa,alphaBaa,alphaHY]
betaA = coef(mod_A)[2]
betaB = coef(mod_B)[2]
betaC = coef(mod_C)[2]
betaAaa = coef(mod_Aaa)[2]
betaBaa = coef(mod_Baa)[2]
betaHY = coef(mod_HY)[2]
beta = [betaA, betaB, betaC,betaAaa,betaBaa,betaHY]
AlphaBeta_Crisis = DataFrame(Alpha=alpha, Beta=beta)


@fmt Real = "{:.6f}"
nwStd(m) = stderror(BartlettKernel(5), m, prewhite = false)
tA = TableCol_tStat("(A Tranche)", mod_A; stderror=nwStd)
setmeta!(tA, :α => "$alphaA")
tB = TableCol_tStat("(B Tranche)", mod_B; stderror=nwStd)
setmeta!(tB, :α => "$alphaB")
tC = TableCol_tStat("(C Tranche)", mod_C; stderror=nwStd)
setmeta!(tC, :α => "$alphaC")
tAaa = TableCol_tStat("(Corp Aaa)", mod_Aaa; stderror=nwStd)
setmeta!(tAaa, :α => "$alphaAaa")
tBaa = TableCol_tStat("(Corp Baa)", mod_Baa; stderror=nwStd)
setmeta!(tBaa, :α => "$alphaBaa")
tHY   = TableCol_tStat("(Corp HY)", mod_HY; stderror=nwStd)
setmeta!(tHY, :α => "$alphaHY")
tbl = regtable(tA,tB,tC,tAaa,tBaa,tHY)
print(to_ascii(tbl,se_pos=:inline,pad=1))



#Post
f_A = @formula(RxEx_A ~ 1 + RxExMkt)
mod_A = lm(f_A,RegData_Post)
f_B = @formula(RxEx_B ~ 1 + RxExMkt)
mod_B = lm(f_B,RegData_Post)
f_C = @formula(RxEx_C ~ 1 + RxExMkt)
mod_C = lm(f_C,RegData_Post)
f_Aaa = @formula(RxExCorpAaa ~ 1 + RxExMkt)
mod_Aaa = lm(f_Aaa,RegData_Post)
f_Baa = @formula(RxCorpBaa ~ 1 + RxExMkt)
mod_Baa = lm(f_Baa,RegData_Post)
f_HY = @formula(RxExCorpHY ~ 1 + RxExMkt)
mod_HY = lm(f_HY,RegData_Post)
alphaA = coef(mod_A)[1].*12.0
alphaB = coef(mod_B)[1].*12.0
alphaC = coef(mod_C)[1].*12.0
alphaAaa = coef(mod_Aaa)[1].*12.0
alphaBaa = coef(mod_Baa)[1].*12.0
alphaHY = coef(mod_HY)[1].*12.0
alpha = [alphaA, alphaB, alphaC,alphaAaa,alphaBaa,alphaHY]
betaA = coef(mod_A)[2]
betaB = coef(mod_B)[2]
betaC = coef(mod_C)[2]
betaAaa = coef(mod_Aaa)[2]
betaBaa = coef(mod_Baa)[2]
betaHY = coef(mod_HY)[2]
beta = [betaA, betaB, betaC,betaAaa,betaBaa,betaHY]
AlphaBeta_Post = DataFrame(Alpha=alpha, Beta=beta)


@fmt Real = "{:.6f}"
nwStd(m) = stderror(BartlettKernel(5), m, prewhite = false)
tA = TableCol_tStat("(A Tranche)", mod_A; stderror=nwStd)
setmeta!(tA, :α => "$alphaA")
tB = TableCol_tStat("(B Tranche)", mod_B; stderror=nwStd)
setmeta!(tB, :α => "$alphaB")
tC = TableCol_tStat("(C Tranche)", mod_C; stderror=nwStd)
setmeta!(tC, :α => "$alphaC")
tAaa = TableCol_tStat("(Corp Aaa)", mod_Aaa; stderror=nwStd)
setmeta!(tAaa, :α => "$alphaAaa")
tBaa = TableCol_tStat("(Corp Baa)", mod_Baa; stderror=nwStd)
setmeta!(tBaa, :α => "$alphaBaa")
tHY   = TableCol_tStat("(Corp HY)", mod_HY; stderror=nwStd)
setmeta!(tHY, :α => "$alphaHY")
tbl = regtable(tA,tB,tC,tAaa,tBaa,tHY)
print(to_ascii(tbl,se_pos=:inline,pad=1))




RxTbl = transform(Sumstat, names(Sumstat,Not(:Return)) .=> ByRow(Float64), :Return=> ByRow(String),renamecols=false)
AlphaBetaTbl = DataFrame( Return = ["β_A","β_B","β_C","β_Aaa","β_Baa","β_HY","α_A","α_B","α_C","α_Aaa","α_Baa","α_HY"], 
                    All=[AlphaBeta_All[:,2]; AlphaBeta_All[:,1]], Pre=[AlphaBeta_Pre[:,2]; AlphaBeta_Pre[:,1]],
                    Crisis=[AlphaBeta_Crisis[:,2]; AlphaBeta_Crisis[:,1]], Post=[AlphaBeta_Post[:,2]; AlphaBeta_Post[:,1]])
transform!(AlphaBetaTbl, names(AlphaBetaTbl,Not(:Return)) .=> ByRow(Float64), :Return=> ByRow(String),renamecols=false)
rename!(AlphaBetaTbl,:Pre=>"2000-2006",:Crisis=>"2007-2009",:Post=>"2010-2020")

SummStatsRxAlphaBeta = [RxTbl;AlphaBetaTbl]
SummStatsRxAlphaBeta = SummStatsRxAlphaBeta[append!(collect(1:12), collect(13:15), collect(19:21), collect((25:30)),collect(31:42)),:]

rowNames = SummStatsRxAlphaBeta[:,1]
colNames = names(SummStatsRxAlphaBeta)[2:end]
data = Matrix(SummStatsRxAlphaBeta[:,2:end])
formatStr = 3
pretty_table(data, colNames, row_names = rowNames, formatters = ft_round(formatStr))


#Results in Table 5
# Note: Rows ordered manually from console output to match Table 5

# ┌─────────────┬────────┬───────────┬───────────┬───────────┐
# │             │    All │ 2000-2006 │ 2007-2009 │ 2010-2020 │
# ├─────────────┼────────┼───────────┼───────────┼───────────┤
# │      RxEx_A │  1.947 │     2.051 │     2.726 │     1.581 │
# │      RxEx_B │  3.099 │     2.314 │     6.625 │     2.482 │
# │      RxEx_C │  4.529 │     3.211 │    10.054 │     3.634 │
# │ RxExCorpAaa │  4.247 │     4.022 │     1.385 │     5.465 │
# │ RxExCorpBaa │  5.141 │     3.863 │     5.054 │     6.233 │
# │  RxExCorpHY │  5.743 │      4.31 │     5.212 │     7.123 │
# ├─────────────┼────────┼───────────┼───────────┼───────────┤
# │    SDevRx_A │  3.188 │     0.876 │     7.639 │     0.874 │
# │    SDevRx_B │  7.902 │     0.768 │    19.496 │     1.384 │
# │    SDevRx_C │ 11.502 │     1.228 │    28.402 │     1.861 │
# │   SDevRxAaa │  5.753 │     4.607 │     8.788 │     5.267 │
# │   SDevRxBaa │   5.94 │     5.425 │      9.94 │     4.288 │
# │    SDevRxHY │  9.237 │     8.034 │    17.172 │     5.491 │
# ├─────────────┼────────┼───────────┼───────────┼───────────┤
# │         β_A │  0.026 │    -0.019 │     0.133 │     -0.01 │
# │         β_B │  0.108 │    -0.015 │     0.428 │    -0.011 │
# │         β_C │  0.147 │     -0.03 │     0.592 │    -0.013 │
# │       β_Aaa │ -0.036 │    -0.036 │     0.094 │    -0.129 │
# │       β_Baa │  0.107 │     0.107 │     0.308 │     0.034 │
# │        β_HY │  0.404 │     0.404 │     0.678 │     0.316 │
# ├─────────────┼────────┼───────────┼───────────┼───────────┤
# │        SR_A │  0.611 │     2.341 │     0.357 │     1.809 │
# │        SR_B │  0.392 │     3.014 │      0.34 │     1.793 │
# │        SR_C │  0.394 │     2.615 │     0.354 │     1.952 │
# │  SR_CorpAaa │  0.738 │     0.873 │     0.158 │     1.037 │
# │  SR_CorpBaa │  0.865 │     0.712 │     0.508 │     1.454 │
# │   SR_CorpHY │  0.622 │     0.537 │     0.304 │     1.297 │
# └─────────────┴────────┴───────────┴───────────┴───────────┘






